//
//  HBVipController.m
//  HeartBeat
//
//  Created by karong on 2023/9/15.
//

#import "HBVipController.h"
#import "HBVipCell.h"
#import "HBGoodsModel.h"
#import "HBPurchaseManager.h"


@interface HBVipController () <HBPurchaseManagerDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation HBVipController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadVipRequest];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mainViewBottom.constant = 0;
        [self.view uploadLayout];
    });
    [HBEvenManager event:@"Member_storage_value" evenLogType:(HBEvenLogType_Show)];
}

- (void)styleDidChange {
    self.view.backgroundColor = UIColor.clearColor;
}

- (void)loadVipRequest {
    WeakSelf
    [HBToast loading];
    [HBRequest loadGoodsListWithGoods_type:2 success:^(NSArray *array) {
        weakSelf.dataArray = array;
        [HBPurchaseManager shared].delegate = weakSelf;
        [HBPurchaseManager shared].products = array;
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (IBAction)payAction:(id)sender {
    HBGoodsModel *goods = self.dataArray[self.selectIndex];
    if (goods.SKProduct) {
        if (appUserCenter.isLogin) {
            [HBPurchaseManager shared].delegate = self;
            [[HBPurchaseManager shared] createOrderWithProduct:goods chapter:self.chapter className:NSStringFromClass(self.class)];
        }
        else {
            [ViewManager showLoginVCInController:self];
        }
        [HBEvenManager event:@"Member_storage_value" evenLogType:(HBEvenLogType_Click) attributes:@{@"member_id":goods.ios_product_id}];
    }
}

#pragma mark - HBPurchaseManagerDelegate
- (void)didAppleReceiveResponse {
    [HBToast dismiss];
    self.selectIndex = 0;
    [self.listView reloadData];
}

- (void)purchaseManager:(HBPurchaseManager *)manager updateStatus:(HBPurchaseStatus)status object:(id)object {
    switch (status) {
        case HBPurchaseStatusSuccess:
        {
            
        }
            break;
        case HBPurchaseStatusCancel:
            [HBToast simple:WSLocalized(@"支付失败")];
            break;
        default:
            break;
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBVipCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBVipCell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row] select:self.selectIndex == indexPath.row];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = 105 + 1;
    size.height = 110 + 3;
    return size;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
    [self.listView reloadData];
}

@end
